package com.leapp.sk.util.im

/**
 * @author  chaohaipeng  on 2018/7/20.
 */
class ImChatServer{
    private object mHolder {
        val instance = ImChatServer()
    }
    companion object {
        fun getInstance(): ImChatServer {
            return mHolder.instance
        }
    }

    /**
     * 初始化图片最大显示宽高
     *
     * @param maxWidth
     * @param maxHeight
     * @param imageWidth
     * @param imageHeight
     * @return
     */
    fun imageWidthHeightCalculate(maxWidth: Int, maxHeight: Int, imageWidth: String, imageHeight: String): IntArray {
        var widthAndHeight = intArrayOf(100, 100)
        try {
            val width = Integer.valueOf(imageWidth)!!
            val height = Integer.valueOf(imageHeight)!!
            widthAndHeight = imageWidthHeightCalculate(maxWidth, maxHeight, width, height)
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return widthAndHeight
    }

    /**
     * 最大宽高计算
     *
     * @param maxWidth
     * @param maxHeight
     * @param imageWidth
     * @param imageHeight
     * @return
     */
    fun imageWidthHeightCalculate(maxWidth: Int, maxHeight: Int, imageWidth: Int, imageHeight: Int): IntArray {
        var imageWidth = imageWidth
        var imageHeight = imageHeight
        val widthAndHeight = intArrayOf(100, 100)
        try {
            if (imageWidth == 0 || imageHeight == 0) {
                imageWidth = 100
                imageHeight = 100
            }
            if (imageWidth > maxWidth) {
                imageHeight = imageHeight * maxWidth / imageWidth
                imageWidth = maxWidth
                if (imageHeight > maxHeight) {
                    imageWidth = imageWidth * maxHeight / imageHeight
                    imageHeight = maxHeight
                }
            } else if (imageHeight > maxHeight) {
                imageWidth = imageWidth * maxHeight / imageHeight
                imageHeight = maxHeight
                if (imageWidth > maxWidth) {
                    imageHeight = imageHeight * maxWidth / imageWidth
                    imageWidth = maxWidth
                }
            }
            widthAndHeight[0] = imageWidth
            widthAndHeight[1] = imageHeight
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return widthAndHeight
    }

}